<template>
    <Teleport to="body">
        <transition name="el-fade-in">
            <div class="infomation-dialog" v-if="visible">
                <div class="infomation-dialog__mask" @click="onClose"></div>
                <div class="infomation-dialog__content">
                    <div class="infomation-dialog__close" @click="onClose">
                        <img :src="`${_oss_}images/icon-close-default.png`" />
                    </div>

                    <div class="title">个人资料</div>

                    <div class="form">
                        <div class="avatar" @click="onUpdateAvatar">
                            <img
                                :src="
                                    userStore.avatar
                                        ? userStore.avatar
                                        : `${_oss_}images/user/default-avatar.png`
                                "
                            />
                            <input
                                type="file"
                                accept="image/*"
                                style="display: none"
                                @change="onAvatarChange"
                                ref="fileInputRef"
                            />

                            <div class="icon-camera-mask">
                                <img
                                    :src="`${_oss_}images/admin/icon-camera.png`"
                                />
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label form-required">用户名：</div>
                            <div class="form-content">
                                <el-input type="text" v-model="form.nickname" />
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label form-required">性别：</div>
                            <div class="form-content">
                                <el-radio-group v-model="form.gender">
                                    <el-radio :label="1">男</el-radio>
                                    <el-radio :label="0">女</el-radio>
                                </el-radio-group>
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label form-required">所在地：</div>
                            <div class="form-content">
                                <el-cascader
                                    :options="regionData"
                                    :props="{ value: 'label' }"
                                    v-model="form.city"
                                    style="width: 200px"
                                >
                                </el-cascader>
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label form-required">
                                所属行业：
                            </div>
                            <div class="form-content">
                                <el-cascader
                                    v-model="form.industry_id"
                                    :options="industryList"
                                    :props="{
                                        value: 'id',
                                        label: 'name',
                                        children: 'child',
                                    }"
                                    v-if="industryList.length"
                                >
                                </el-cascader>
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label">个性签名：</div>
                            <div class="form-content">
                                <el-input
                                    type="text"
                                    v-model="form.personal_ignature"
                                />
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label">爱好：</div>
                            <div class="form-content">
                                <el-input
                                    type="textarea"
                                    v-model="form.hobby"
                                    :rows="2"
                                    resize="none"
                                />
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label">绑定手机：</div>
                            <div class="form-content">
                                {{ userStore.userInfo.mobile }}
                                <div class="btn" @click="onUpdatePhone">
                                    修改
                                </div>
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label">密码：</div>
                            <div class="form-content">
                                ************
                                <div class="btn" @click="onUpdatePassword">
                                    修改
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="save-btn" @click="onSubmit">保存</div>
                </div>
            </div>
        </transition>
    </Teleport>

    <LazyDialogUpdatePhoneP ref="updatePhoneDialogRef" />
    <LazyDialogAvatarCropperP ref="avatarCropperRef" />
    <LazyDialogAvatarSelectP ref="avatarDialogRef" @diy="onDiy" />
</template>
<script setup>
import { useContent } from ".";
import { regionData } from "element-china-area-data";

const userStore = useUserStore();
const {
    form,
    industryList,
    updatePhoneDialogRef,
    GetList,
    onSubmit,
    onGetUserInfo,
    onUpdatePassword,
    onUpdatePhone,
} = useContent();
const visible = ref(false);
const avatarCropperRef = ref(null);
const avatarDialogRef = ref(null);

const onClose = () => {
    visible.value = false;
};

GetList();

const onAvatarChange = (e) => {
    const file = e.target.files[0];
    avatarCropperRef.value.show(window.URL.createObjectURL(file), file.name);
    e.target.value = "";
};

const onUpdateAvatar = () => {
    avatarDialogRef.value.show();
};

const onDiy = () => {
    fileInputRef.value.click();
};

defineExpose({
    show() {
        visible.value = true;
        onGetUserInfo();
    },
});
</script>
<style lang="scss" scoped>
.infomation-dialog {
    position: fixed;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    z-index: 999;

    &__mask {
        position: relative;
        width: 100%;
        height: 100%;
        background-color: rgba($color: #000000, $alpha: 0.5);
    }

    &__content {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 500px;
        background-color: #fff;
        transform: translate(-50%, -50%);
        border-radius: 10px;
        padding: 0 20px 28px;
    }

    &__close {
        position: absolute;
        top: 22px;
        right: 19px;
        width: 17px;
        height: 17px;
        cursor: pointer;
    }

    .avatar {
        position: relative;
        margin-top: 20px;
        width: 80px;
        height: 80px;
        margin: 20px auto;
        border-radius: 4px;
        overflow: hidden;

        img {
            width: 100%;
            height: 100%;
        }
    }

    .title {
        font-weight: bold;
        font-size: 18px;
        color: #333333;
        line-height: 64px;
        text-align: center;
        border-bottom: 1px solid #f1f1f1;
    }

    .icon-camera-mask {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: rgba($color: #000000, $alpha: 0.5);
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;

        img {
            width: 20px;
            height: 20px;
        }
    }

    .form {
        &-required {
            position: relative;

            &::before {
                content: "*";
                color: #ff4941;
                font-weight: bold;
                margin-right: 2px;
            }
        }

        &-item {
            display: flex;
            align-items: center;
            flex: 1;
            margin-bottom: 13px;
        }

        &-label {
            width: 120px;
            text-align: right;
            font-size: 14px;
            color: #666;
            margin-right: 20px;
        }

        &-content {
            display: flex;
            align-items: center;
            font-size: 14px;
            color: #666;
            width: 100%;
        }

        :deep() {
            .el-cascader,
            .el-input,
            .el-textarea {
                width: 100% !important;
            }

            .el-textarea {
                resize: none;
            }
        }

        .btn {
            width: 50px;
            height: 26px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            background: #5085fb;
            border-radius: 4px;
            font-size: 14px;
            color: #ffffff;
            margin-left: 19px;
            cursor: pointer;
        }
    }

    .save-btn {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100px;
        height: 38px;
        background: #5085fb;
        border-radius: 8px;
        font-weight: bold;
        font-size: 16px;
        color: #ffffff;
        margin: 30px auto 0;
        cursor: pointer;
    }
}

:deep(.no-icon) {
    width: 100%;
    .el-input__prefix {
        display: none !important;
    }
}
</style>
